package com.wuyb.config;

import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * SocketIO配置类
 *
 * @Author: wyb
 * @Date: 2024/01/07/13:42
 * @Description:
 */
@Configuration
public class SocketIOConfig {

    @Value("${socketio.host}")
    private String host;

    @Value("${socketio.port}")
    private Integer port;

    @Value("${socketio.bossCount}")
    private Integer bossCount;

    @Value("${socketio.workCount}")
    private Integer workCount;

    @Value("${socketio.allowCustomRequests}")
    private Boolean allowCustomRequests;

    @Value("${socketio.upgradeTimeout}")
    private Integer upgradeTimeout;

    @Value("${socketio.pingTimeout}")
    private Integer pingTimeout;

    @Value("${socketio.pingInterval}")
    private Integer pingInterval;


    /**
     * SocketIO配置
     * @return
     */
    @Bean
    public SocketIOServer socketIOServer() {
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);
        com.corundumstudio.socketio.Configuration configuration = new com.corundumstudio.socketio.Configuration();
        configuration.setSocketConfig(socketConfig);
        configuration.setHostname(host);
        configuration.setPort(port);
        configuration.setBossThreads(bossCount);
        configuration.setWorkerThreads(workCount);
        configuration.setAllowCustomRequests(allowCustomRequests);
        configuration.setUpgradeTimeout(upgradeTimeout);
        configuration.setPingTimeout(pingTimeout);
        configuration.setPingInterval(pingInterval);
        return new SocketIOServer(configuration);
    }


    /**
     * 用于扫描netty的注解，比如@Onconnect、@OnEvent
     *
     * @return
     */
    @Bean
    public SpringAnnotationScanner springAnnotationScanner() {
        return new SpringAnnotationScanner(socketIOServer());
    }
}
